package com.example.myapplication.utils;

import com.google.gson.Gson;
import com.lxj.xpopup.util.TextUtils;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

import java.util.HashMap;
import java.util.Map;

public class PreferencesUtils {
    /**
     * 以表名缓存各张表
     */
    private static final HashMap<String, PreferencesUtils> SP_UTILS_MAP = new HashMap<>();

    private Preferences preferences;
    private Gson gson;

    private PreferencesUtils(String spName,Context context){
        DatabaseHelper databaseHelper = new DatabaseHelper(context.getApplicationContext());
        preferences=databaseHelper.getPreferences(spName);
        gson=new Gson();
    }

    public static PreferencesUtils getInstance(String spName, Context context) {
        PreferencesUtils spUtils = SP_UTILS_MAP.get(spName);
        if (spUtils == null) {
            synchronized (PreferencesUtils.class) {
                if (spUtils == null) {
                    spUtils = new PreferencesUtils(spName,context);
                    SP_UTILS_MAP.put(spName, spUtils);
                }
            }
        }
        return spUtils;
    }

    public PreferencesUtils put(String key,String value){
        preferences.putString(key,value);
        preferences.flushSync();
        return this;
    }

    public PreferencesUtils put(String key,int value){
        preferences.putInt(key,value);
        preferences.flushSync();
        return this;
    }

    public PreferencesUtils put(String key,boolean value){
        preferences.putBoolean(key,value);
        preferences.flushSync();
        return this;
    }

    public PreferencesUtils put(String key,float value){
        preferences.putFloat(key,value);
        preferences.flushSync();
        return this;
    }

    /**
     * 存储对象
     * @param key
     * @param o
     * @return
     */
    public PreferencesUtils put(String key,Object o){
        if(gson==null)gson=new Gson();
        preferences.putString(key,gson.toJson(o));
        preferences.flushSync();
        return this;
    }
    /**
     * 获取对象
     *
     * @param key
     * @param clazz
     * @return
     */
    public <T> T get( String key, Class<T> clazz) {
        String json = getString(key);
        if (TextUtils.isEmpty(json)) {
            return null;
        }
        try {
            if (gson == null) {
                gson = new Gson();
            }
            return gson.fromJson(json, clazz);
        } catch (Exception e) {
            return null;
        }
    }
    public String getString(String key){

        return preferences.getString(key,"");
    }

    public Preferences getPreferences() {
        return preferences;
    }

    public Map<String, ?> getAll(){

        return preferences.getAll();
    }
    public void delete(String key){
        preferences.delete(key);
        preferences.flushSync();
    }
    public void clear(){
        preferences.clear();
        preferences.flushSync();
    }
    public int getInt(String key){
        return preferences.getInt(key,0);
    }


    public boolean getBoolean(String key){
        return preferences.getBoolean(key,false);
    }


    public float getFloat(String key){
        return preferences.getFloat(key,0.0f);
    }

}
